'''
regex.py

Copyright 2009 Xavier Mendez Navarro aka Javi

This file is part of pysqlin

pysqlin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

pysqlin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with pysqlin; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
'''

import logging
import re

from framework.myexceptions import MyException
from framework.baseclass import BInjector
from framework.interfaces import IInjector

class SimpleRegex(BInjector, IInjector):
    """Launchs and controls the injection threads"""

    def __init__(self, api):
	BInjector.__init__(self, api)

	self.__logger = logging.getLogger('framework.mythreading.SimpleRegex')

    def name(self): return "SimpleRegex"
    def description(self): return "Exploits \"verbosed\" sql injections"
    def parameters(self): 
	return dict(
	    regex = ('', 'Regex to find result.'),
	    noresult = ('','String indicating that no result has been fetched.'),
	)

    def is_threadable(self): return True
    def cardinality(self): return 1

    def check_parameters(self, data):
	try:
	    self.parser = re.compile(data['regex'],re.MULTILINE|re.DOTALL)
	except Exception:
	    raise MyException("Invalid regex expression")

    def cancel_job(self):
	pass

    def launch_job(self, from_row, to_row):
	self.__logger.debug("launch_job. START")

	code, content = self.sqli.perform(row=from_row)

	# get the data
	for i in self.parser.findall(content):
	    if self.sqli.injection_data['noresult'] and i == self.sqli.injection_data['noresult']:
		queue.put_normal(None)
		return

	    self.put_results(from_row, i)
	    return

	self.no_results(from_row)

def load(api):
    return SimpleRegex(api)
